package com.hym.hymlistview_x;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.hym.myrrefreshlistview.R;

public class HymListViewHeader extends LinearLayout {

	private LinearLayout mContainer;
	private ImageView mArrowImageView;
	private ProgressBar mProgressBar;
	private TextView mHintTextView;
	/** Header当前所处的状态 */
	private int mState = STATE_NORMAL;
	/** 箭头向上翻转的动画 */
	private Animation mRotateUpAnim;
	/** 箭头向下翻转的动画 */
	private Animation mRotateDownAnim;
	/** 动画的间隔时间 */
	private final int ROTATE_ANIM_DURATION = 180;

	public final static int STATE_NORMAL = 0;
	public final static int STATE_READY = 1;
	public final static int STATE_REFRESHING = 2;

	public HymListViewHeader(Context context) {
		super(context);
		initView(context);
	}

	public HymListViewHeader(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView(context);
	}

	public HymListViewHeader(Context context, AttributeSet attrs,
			int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		initView(context);
	}

	private void initView(Context context) {
		// 初始情况下，设置下拉刷新的view的高度为0
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, 0);
		mContainer = (LinearLayout) LayoutInflater.from(context).inflate(
				R.layout.xlistview_header, null);
		// 将header的布局添加自定义的布局中
		addView(mContainer, lp);
		setGravity(Gravity.BOTTOM);
		mArrowImageView = (ImageView) findViewById(R.id.xlistview_header_arrow);
		mHintTextView = (TextView) findViewById(R.id.xlistview_header_hint_textview);
		mProgressBar = (ProgressBar) findViewById(R.id.xlistview_header_progressbar);

		mRotateUpAnim = new RotateAnimation(0.0f, -180.0f,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		mRotateUpAnim.setDuration(ROTATE_ANIM_DURATION);
		mRotateUpAnim.setFillAfter(true);
		mRotateDownAnim = new RotateAnimation(-180.0f, 0.0f,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		mRotateDownAnim.setDuration(ROTATE_ANIM_DURATION);
		mRotateDownAnim.setFillAfter(true);

	}

	/**
	 * 给header设置状态
	 */
	public void setState(int state) {
		if (state == mState)
			return;
		if (state == STATE_REFRESHING) {// 显示刷新进度条
			mArrowImageView.clearAnimation();
			mArrowImageView.setVisibility(View.INVISIBLE);
			mProgressBar.setVisibility(View.VISIBLE);
		} else {// 显示箭头图片
			mArrowImageView.setVisibility(View.VISIBLE);
			mProgressBar.setVisibility(View.INVISIBLE);
		}

		//状态切换过程中所做的相关ui的处理
		switch(state){
		case STATE_NORMAL:
			//STATE_NORMAL->STATE_READY
			if (mState == STATE_READY) {
				mArrowImageView.startAnimation(mRotateDownAnim);
			}
			if (mState == STATE_REFRESHING) {
				mArrowImageView.clearAnimation();
			}
			mHintTextView.setText(R.string.hymlistview_header_hint_normal);
			break;
		case STATE_READY:
			if (mState != STATE_READY) {
				mArrowImageView.clearAnimation();
				mArrowImageView.startAnimation(mRotateUpAnim);
				mHintTextView.setText(R.string.hymlistview_header_hint_ready);
			}
			break;
		case STATE_REFRESHING:
			mHintTextView.setText(R.string.hymlistview_header_hint_loading);
			break;
			default:
		}
		
		mState = state;
	}
	/**
	 * 设置headerView的可见的高度
	 */
	public void setVisibleHeight(int height){
		if(height<0){
			height=0;
		}
		LinearLayout.LayoutParams lp=(LinearLayout.LayoutParams)mContainer.getLayoutParams();
		lp.height=height;
		mContainer.setLayoutParams(lp);
	}
	
	/**
	 * 获取HeaderView的可见高度
	 */
	public int getVisiableHeight(){
		return mContainer.getLayoutParams().height;
	}
}
